package com.cn.lf.wx.dacs.bussiness.author.serviceImpl;

import com.cn.lf.wx.dacs.bussiness.author.entity.AuthMenu;
import com.cn.lf.wx.dacs.bussiness.author.entity.AuthRole;
import com.cn.lf.wx.dacs.bussiness.author.entity.AuthRoleMenu;
import com.cn.lf.wx.dacs.bussiness.author.entity.AuthUser;
import com.cn.lf.wx.dacs.bussiness.author.dao.AuthRoleMapper;
import com.cn.lf.wx.dacs.bussiness.author.bean.ReturnEntity;
import com.cn.lf.wx.dacs.bussiness.author.bean.req.RoleHaveMenus;
import com.cn.lf.wx.dacs.bussiness.author.service.IAuthRoleService;
import com.cn.lf.wx.dacs.bussiness.author.utils.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-03-01
 */
@Service
public class AuthRoleServiceImpl extends ServiceImpl<AuthRoleMapper, AuthRole> implements IAuthRoleService {
	
	@Autowired
	private AuthRoleMenuServiceImpl authRoleMenuService;
	
	@Autowired
	private AuthUserServiceImpl authUserService;
	
	@Autowired
	private AuthMenuServiceImpl authMenuServiceImpl;
	
	@Value("${nxpcs.default.super.admin.id}")
	protected String superAdminId;


    public ReturnEntity roles() {
        List<AuthRole> adminRoles = list();
        return ReturnEntity.SUCCESS("获取成功").setData(adminRoles);
    }

    public ReturnEntity createRole(RoleHaveMenus adminRole) {
        if (StringUtil.isEmpty(adminRole.getRoleKey())) {
            return ReturnEntity.FAILED("角色Key不能为空");
        }
        if (StringUtil.isEmpty(adminRole.getRoleName())) {
            return ReturnEntity.FAILED("角色名称不能为空");
        }
        AuthRole selectOne = getOne(new QueryWrapper<AuthRole>().eq("role_key", adminRole.getRoleKey()));
        if (selectOne != null) {
            return ReturnEntity.FAILED("角色key已经存在");
        }
        AuthRole role = new AuthRole();
        role.setRoleKey(adminRole.getRoleKey());
        role.setRoleName(adminRole.getRoleName());
        save(role);
        List<String> menus = adminRole.getMenus();
        List<AuthRoleMenu> roleMenus = new ArrayList<>();
        if (!menus.isEmpty()) {
            for (String menu : menus) {
                AuthRoleMenu roleMenu = new AuthRoleMenu();
                roleMenu.setRoleId(role.getId());
                roleMenu.setMenuId(menu);
                roleMenus.add(roleMenu);
            }
        }
        int insertAll = authRoleMenuService.getBaseMapper().insertAll(roleMenus);
        if (insertAll > 0) {
            return ReturnEntity.SUCCESS("创建成功");
        }
        return ReturnEntity.FAILED("创建失败");
    }

    public ReturnEntity changeRole(RoleHaveMenus roleHaveMenus) {
        if (superAdminId.equals(roleHaveMenus.getRoleId())) {
            return ReturnEntity.FAILED("超级管理员不允许修改");
        }
        AuthRole adminRole = new AuthRole();
        adminRole.setId(roleHaveMenus.getRoleId());
        adminRole.setRoleName(roleHaveMenus.getRoleName());
        if (!updateById(adminRole)) {
            return ReturnEntity.FAILED("修改失败");
        }
        List<String> menus = roleHaveMenus.getMenus();
        List<AuthRoleMenu> roleMenus = new ArrayList<>();
        authRoleMenuService.remove(new QueryWrapper<AuthRoleMenu>().eq("role_id", roleHaveMenus.getRoleId()));

        if (!menus.isEmpty()) {
            for (String menu : menus) {
                AuthRoleMenu roleMenu = new AuthRoleMenu();
                roleMenu.setRoleId(roleHaveMenus.getRoleId());
                roleMenu.setMenuId(menu);
                roleMenus.add(roleMenu);
            }
        }
        int insertAll = authRoleMenuService.getBaseMapper().insertAll(roleMenus);
        if (insertAll > 0) {
            return ReturnEntity.SUCCESS("修改成功");
        }
        return ReturnEntity.FAILED("修改失败");
    }

    @Transactional
    public ReturnEntity deleteRole(String id) {
    	System.out.println(id);
        if (superAdminId.equals(id)) {
            return ReturnEntity.FAILED("超级管理员不允许删除");
        }
        long count = authUserService.count(new QueryWrapper<AuthUser>().eq("role_id", id));
        if (count > 0) {
            return ReturnEntity.FAILED("该角色使用中");
        }
//        删除角色
        removeById(id);
        boolean flag = authRoleMenuService.remove(new QueryWrapper<AuthRoleMenu>().eq("role_id", id));
        if (flag) {
            return ReturnEntity.SUCCESS("删除成功");
        }
        return ReturnEntity.FAILED("删除失败");
    }

	public ReturnEntity roleMenus(String rid) {
        List<AuthMenu> adminMenus;
//      超级管理员 具有所有权限
      if (superAdminId.equals(rid)) {
          adminMenus = authMenuServiceImpl.list(new QueryWrapper<AuthMenu>().orderByDesc("sort"));
      } else {
          adminMenus = baseMapper.roleMenus(rid);
      }
      return ReturnEntity.SUCCESS("菜单获取成功.").setData(adminMenus);
	}

}
